<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2018/12/28
 * Time: 14:06
 */

namespace app\api\controller;


use think\Db;

class Aftersales extends Base
{
    /**
     * 售后分类列表
     * cate_type  1 说明书  2 安装调试视频  3 故障维修视频  4 保养视频  5 产品信息  6 厂家信息
     * page 当前页码
     * @return \think\response\Json
     */
    public function cateList(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        $where = [];
        $user_id = ',' . $params['user_id'] . ',';
        $where['instructions_status'] = $params['cate_type'];
        $where['instructions_user_id'] = ['like', '%' . $user_id . '%'];
        //分类列表
        $list = Db::table('up_instructions')
            ->field('instructions_id, instructions_name, instructions_time,
            instructions_suf, instructions_type')
            ->where($where)
            ->order('instructions_time desc')
            ->page($params['page'], 10)
            ->select();
        
        if(empty($list)){
            return json(['status' => 200, 'msg' => []]);
        }else{
            return json(['status' => 200, 'msg' => $list]);
        }
    }
    
    /**
     * 售后分类列表---新需求，供缓存用
     * @return \think\response\Json
     */
    public function cateListOne(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        $where = [];
        $user_id = ',' . $params['user_id'] . ',';
        $where['instructions_user_id'] = ['like', '%' . $user_id . '%'];
        $instructions_status = Db::table('up_instructions')
            ->field('instructions_status')
            ->where($where)
            ->group('instructions_status')
            ->select();
        foreach($instructions_status as $k => &$v){
            $sun = Db::table('up_instructions')
                ->field([
                    'instructions_id',
                    'instructions_name',
                    'instructions_status',
                    'instructions_suf',
                    'instructions_type',
                    'instructions_url'
                ])
                ->where('instructions_status', $v['instructions_status'])
                ->where($where)
                ->order('instructions_time desc')
                ->select();
            if($sun){
                foreach($sun as $ke => &$va){
                    if($va['instructions_url'] != ''){
                        $va['instructions_url'] = http_type() . $va['instructions_url'];
                    }
                }
            }
            $v['sun'][] = $sun;
        }
        if(empty($instructions_status)){
            return json(['status' => 200, 'msg' => []]);
        }else{
            return json(['status' => 200, 'msg' => $instructions_status]);
        }
    }
    
    /**
     * 详情
     * instructions_id  文件ID
     * @return \think\response\Json
     */
    public function aftersalesDetail(){
        $params = input('param.');
        $is_exist = Db::table('up_factory_member')->where('m_id', $params['user_id'])->find();
        if(!$is_exist){
            return json(['status' => 1, 'msg' => '该用户不存在']);
        }
        $detail = Db::table('up_instructions')
            ->field(['instructions_url', 'instructions_name', 'instructions_suf', 'instructions_type'])
            ->where('instructions_id', $params['instructions_id'])
            ->find();
        if(!$detail){
            return json(['status' => 0, 'msg' => '文件不存在']);
        }
        //如果文件是图片，需要转换成数组形式
//        if($detail['instructions_type'] == 5){
//            //转换成数组
//            $arr_img = trim($detail['instructions_url'], ',');
//            $detail['instructions_url'] = explode(',', $arr_img);
//            //拼接url
//            foreach($detail['instructions_url'] as $key => &$va){
//                $va = http_type() . '/' . $va;
//            }
//        }else{
//            $detail['instructions_url'] = http_type() . '/' . $detail['instructions_url'];
//        }
        $detail['instructions_url'] = http_type() . $detail['instructions_url'];
        return json(['status' => 200, 'msg' => $detail]);
    }
}